<?php
/**
 * Created by PhpStorm.
 * User: 陈阳
 * Date: 2021/1/30
 * Time: 9:21
 */
namespace app\admin\model;
use think\model;
class Brand extends Model
{
    public function getRecommendedAttr($value){
        if(\think\facade\Config::has('search.recommended')){
            $config = \think\facade\Config::get('search.recommended');
            return $config[$value];
        }
    }
    public function getIfshowAttr($value){
        if(\think\facade\Config::has('search.if_show')){
            $config = \think\facade\Config::get('search.if_show');
            return $config[$value];
        }
    }

    public function searchBrandNameAttr($query,$value){
        if(''!==$value){
            $query->where('brand_name','like','%'.$value.'%');
        }
    }
    public function searchRecomAttr($query,$value){
        if(isset($value) && is_array($value)){
            $query->whereIn('recommended',$value);
        }

        if(isset($value) && is_scalar($value) && '200' != $value){
            $query->where('recommended',$value);
        }
    }
    public function searchShowAttr($query,$value){
        if(is_array($value) && 1 == count($value)) {
            $query->where('if_show',$value['0']);
        }

    }
    public function searchBrandLogoAttr($query,$value){
        if(is_array($value) && 1 == count($value) && 1 == $value[0]){
            $query->where('brand_logo','<>','')->whereNotNull('brand_logo');
        }
        if(is_array($value) && 1 == count($value) && 0 == $value[0]){
              $query->where(function($query){
               $query->where('brand_logo','=','')->whereOr('brand_logo','null');
           });
        }

    }
    public static function pai($data = [],$query = [],$page = true){

        $searchAttr=array_keys($data);
        $model = self::field(true)
                      ->withSearch($searchAttr,$data);

        if($page){
            $list_rows = \think\facade\Config::has('view.list_rows')?\think\facade\Config::get('view.list_rows'):15;
            return $model->paginate([
                'list_rows' => $list_rows,//分页的配置参数
                'query'     => $query,
            ]);
        }else{
            return $model->select();
        }
    }
    public static function pin(){
        $model = self::field(['brand_id','brand_name']);
        return $model->select();
    }







    public static function pais($data = [],$query = [],$page = true){

        $model = self::field(true);


        if(isset($data['recom']) && is_array($data['recom'])){
            $model->whereIn('recommended',$data['recom']);
        }

        if(isset($data['recom']) && is_scalar($data['recom']) && '200' != $data['recom']){
            $model->where('recommended',$data['recom']);
        }
        if(is_array($data['show']) && 1 == count($data['show'])) {
            $model->where('if_show',$data['show']['0']);
        }
        if(is_array($data['brand_logo']) && 1 == count($data['brand_logo']) && 1 == $data['brand_logo'][0]){
            $model->where('brand_logo','<>','')->whereNotNull('brand_logo');
        }
        if(is_array($data['brand_logo']) && 1 == count($data['brand_logo']) && 0 == $data['brand_logo'][0]){
               $model->where(function($query){
               $query->where('brand_logo','=','')->whereOr('brand_logo','null');
           });
        }
        if($page){
            $list_rows = \think\facade\Config::has('view.list_rows')?\think\facade\Config::get('view.list_rows'):15;
            return $model->paginate([
                'list_rows' => $list_rows,//分页的配置参数
                'query'     => $query,
            ]);
        }else{
            return $model->select();
        }
    }

}